package com.xm.concurrent.base;

import jdk.internal.org.objectweb.asm.TypeReference;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * 4.3.4 管道输入/输出流
 *
 * @author xiaoming
 * @date 2021-05-26 20:46
 */
public class Piped {
    public static void main(String[] args) throws IOException {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        // 输出流连接输入流
        out.connect(in);

        Thread printThread = new Thread(new Print(in), "PrintThread");
        printThread.start();

        int receive = 0;
        try {
            while ((receive = System.in.read()) != -1) {
                out.write(receive);
            }
        } finally {
            out.close();
        }



    }

    static class Print implements Runnable {
        private PipedReader in;

        public Print(PipedReader in) {
            this.in = in;
        }

        @Override
        public void run() {
            int receive = 0;
            while (true) {
                try {
                    if ((receive = in.read()) != -1) {
                        System.out.print((char) receive);
                    };

                } catch (IOException e) {
                }

            }
        }
    }

}
